* ======================================
* -------- Max. eff. Candidates --------
* ======================================


* Politicians
* ===========

	use $dta\PanelPoliticians.dta, clear
	
	* Restrict to immigrants
	rename Polit_id idPers	
	rename Polit_Run cand
	rename Polit_knr knr
	merge m:1 idPers using $dta/ImportPers.dta, nogen keep(master matched) ///
		keepusing(immigrant birthcountry)
	keep if immigrant == 1

	* Share of gt's total candidacies in municipality mt
	bysort birthcountry knr year: egen nCand_gmt = sum(cand)
	bysort birthcountry year: egen nCand_gt = sum(cand)
	gen pCand_gmt = nCand_gmt/nCand_gt

	collapse pCand_gmt, by(birthcountry knr year)

	tempfile candidates
	save `candidates'


	
* Population at gmt level
* =======================

	use $dta\Population.dta, clear
	merge m:1 idPers using $dta\NetworkImm2.dta, keep(matched) nogen ///
		keepusing(Imm2net)

	* Compute n's
	bysort Imm2net knr year: 		gen n_gomt = _N
	bysort year Imm2net: 			gen n_got = _N
	bysort year birthcountry knr: 	gen n_gmt = _N
	bysort year birthcountry: 		gen n_gt = _N

	* Compute efficiency (fraction inside per gomt)
	gen eff_gomt = n_gomt/n_got

	* Import political candidates
	merge m:1 birthcountry knr year using `candidates', keep(master matched) nogen
	replace pCand_gmt = 0 if pCand_gmt == .


	* Generate estimation variables 
	gen s_gmt = n_gmt/n_gt		 	 // share of group's pop in municipality m
	gen s2_gmt = s_gmt^2 			 // second order
	gen s3_gmt = s_gmt^3 			 // third order
	gen s4_gmt = s_gmt^4 			 // fourth order
	gen pCand100_gmt = pCand_gmt*100 // percentize
	egen numbc = group(birthcountry) // numeric birthcountry
	xtset numbc
	
	
* Estimate models w/ increasing sample restriction (occ-birthcountry)
* ===================================================================

	* Generate variables to store estimates
	gen b_b = .
	gen ciu_b = .
	gen cil_b = .
	gen bw_b = .		
	local index = 1 // row index
	
	forvalues n = 0(250)5000{
		
		preserve
		
		drop if n_gt < `n'
		drop if n_got < 10
	
		* Compute max efficiency and collapse to gomt level
		bysort knr birthcountry year: egen maxeff_gmt = max(eff_gomt)
		collapse pCand100_gmt maxeff s_gmt, by(knr numbc year)
			
		* standardize maxeff
		qui sum maxeff
		local mean = r(mean)
		local sd = r(sd)
		gen maxeff_std = (maxeff - `mean') / `sd'
	
		qui xtreg pCand100_gmt maxeff_std s_gmt, fe cluster(numbc)
		local coef = _b[max]
		local se = _se[max]
		local ci_u = `coef' + 1.96*`se'
		local ci_l = `coef' - 1.96*`se'
		restore
		
		replace b_b = `coef' in `index'
		replace ciu_b = `ci_u' in `index'
		replace cil_b = `ci_l' in `index'
		replace bw_b = `n' in `index'
		local index = `index' + 1 // increment

	}



* Estimate models w/ increasing sample restriction (birthcountry)
* ===============================================================

	* Generate variables to store estimates
	gen b_bo = .
	gen ciu_bo = .
	gen cil_bo = .	
	gen bw_bo = .	
	local index = 1

	forvalues n = 0(5)100{
	
		preserve
	
		drop if n_gt < 1000
		drop if n_got < `n'
	
		* Compute max efficiency and collapse to gomt level
		bysort knr birthcountry year: egen maxeff_gmt = max(eff_gomt)
		collapse pCand100_gmt maxeff s_gmt, by(knr numbc year)
			
		* standardize maxeff
		qui sum maxeff
		local mean = r(mean)
		local sd = r(sd)
		gen maxeff_std = (maxeff - `mean') / `sd'
	
		qui xtreg pCand100_gmt maxeff_std s_gmt, fe cluster(numbc)
		local coef = _b[max]
		local se = _se[max]
		local ci_u = `coef' + 1.96*`se'
		local ci_l = `coef' - 1.96*`se'
		restore
		
		replace b_bo = `coef' in `index'
		replace ciu_bo = `ci_u' in `index'
		replace cil_bo = `ci_l' in `index'
		replace bw_bo = `n' in `index'
		local index = `index' + 1 // increment
		
	}
	

	
* Sensitivity plots
* =================

	twoway (rarea ciu_b cil_b bw_b, col(gray%30)) ///
		(line b_b bw_b,  lcol(cranberry) lpattern(solid) lw(thick)), ///
		xtitle(Birthcountry population cut-off, size(medlarge)) ///
		xlab(, labsize(medium)) ///
		xline(1000, lpattern(dash) lw(medthick)) ///
		ytitle("Coefficient estimate", size(medlarge)) ///
		ylab(-0(0.1)0.6, labsize(medium)) ///
		legend(off) ///
		scheme(plotplain) name(b, replace)	
	
	twoway (rarea ciu_bo cil_bo bw_bo, col(gray%30)) ///
		(line b_bo bw_bo,  lcol(cranberry) lpattern(solid) lw(thick)), ///
		xtitle(Birthcountry-occupation population cut-off, size(medlarge)) ///
		xlab(, labsize(medium)) ///
		xline(10, lpattern(dash) lw(medthick)) ///
		ytitle("Coefficient estimate", size(medlarge)) ///
		ylab(-0(0.1)0.6, labsize(medium)) ///
		legend(off) ///
		scheme(plotplain) name(bo, replace)
			
	graph combine b bo, ycommon scheme(plotplain) xsize(8)
	graph export $figs/MaxeffSensitivity.pdf, replace

